﻿using AssistCore;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Helper
{
    public enum DDKeys
    {
        None = 0,
        ESC = 100,
        F1 = 101,
        F2 = 102,
        F3 = 103,
        F4 = 104,
        F5 = 105,
        F6 = 106,
        F7 = 107,
        F8 = 108,
        F9 = 109,
        F10 = 110,
        F11 = 111,
        F12 = 112,
        /// <summary>
        /// 波浪号
        /// </summary>
        Tilde = 200,
        D1 = 201,
        D2 = 202,
        D3 = 203,
        D4 = 204,
        D5 = 205,
        D6 = 206,
        D7 = 207,
        D8 = 208,
        D9 = 209,
        D0 = 210,
        /// <summary>
        /// 减号（-）
        /// </summary>
        Minus = 211,
        /// <summary>
        /// //等号（=）
        /// </summary>
        Plus = 212,
        /// <summary>
        /// 反斜杠（\）
        /// </summary>
        Backslash = 213,
        BackSpace = 214,
        /// <summary>
        /// TAB键
        /// </summary>
        Tab = 300,
        Q = 301,
        W = 302,
        E = 303,
        R = 304,
        T = 305,
        Y = 306,
        U = 307,
        I = 308,
        O = 309,
        P = 310,
        /// <summary>
        /// 大括号左（[）
        /// </summary>
        OpenBrackets = 311,
        /// <summary>
        /// 大括号右（]）
        /// </summary>
        CloseBrackets = 312,
        Enter = 313,
        /// <summary>
        /// 大写锁定键
        /// </summary>
        CapsLock = 400,
        A = 401,
        S = 402,
        D = 403,
        F = 404,
        G = 405,
        H = 406,
        J = 407,
        K = 408,
        L = 409,
        /// <summary>
        /// 分号（;）
        /// </summary>
        Semicolon = 410,
        /// <summary>
        /// 单引号（'）
        /// </summary>
        SingleQuote = 411,
        /// <summary>
        /// 左Shift
        /// </summary>
        LShift = 500,
        Z = 501,
        X = 502,
        C = 503,
        V = 504,
        B = 505,
        N = 506,
        M = 507,
        /// <summary>
        /// 逗号（,）
        /// </summary>
        Comma = 508,
        /// <summary>
        /// 句号（。）
        /// </summary>
        Period = 509,
        /// <summary>
        /// 问号（?）
        /// </summary>
        Question = 510,
        /// <summary>
        /// 右Shift
        /// </summary>
        RShift = 511,
        LCtrl = 600,
        LWin = 601,
        LAlt = 602,
        Space = 603,
        RAlt = 604,
        RWin = 605,
        Apps = 606,
        RCtrl = 607,
        Print = 700,
        Scroll = 701,
        Pause = 702,
        INS = 703,
        HOME = 704,
        PageUp = 705,
        Delete = 706,
        END = 707,
        PageDown = 708,
        UP = 709,
        Left = 710,
        Down = 711,
        Right = 712,
        NumPad0 = 800,
        NumPad1 = 801,
        NumPad2 = 802,
        NumPad3 = 803,
        NumPad4 = 804,
        NumPad5 = 805,
        NumPad6 = 806,
        NumPad7 = 807,
        NumPad8 = 808,
        NumPad9 = 809,
        NumLock = 810,
        Divide = 811,
        Multiply = 812,
        Subtract = 813,
        Add = 814,
        Enter2 = 815,
        Point2 = 816,
    }

    public class KeyCodeMap
    {
        private static List<Keys> mapList = new List<Keys>(new Keys[1000]);
        public static void Init()
        {
            mapList.Insert((int)DDKeys.None, Keys.None);

            mapList.Insert((int)DDKeys.ESC, Keys.Escape);
            mapList.Insert((int)DDKeys.F1, Keys.F1);
            mapList.Insert((int)DDKeys.F2, Keys.F2);
            mapList.Insert((int)DDKeys.F3, Keys.F3);
            mapList.Insert((int)DDKeys.F4, Keys.F4);
            mapList.Insert((int)DDKeys.F5, Keys.F5);
            mapList.Insert((int)DDKeys.F6, Keys.F6);
            mapList.Insert((int)DDKeys.F7, Keys.F7);
            mapList.Insert((int)DDKeys.F8, Keys.F8);
            mapList.Insert((int)DDKeys.F9, Keys.F9);
            mapList.Insert((int)DDKeys.F10, Keys.F10);
            mapList.Insert((int)DDKeys.F11, Keys.F11);
            mapList.Insert((int)DDKeys.F12, Keys.F12);

            mapList.Insert((int)DDKeys.Tilde, Keys.Oemtilde);
            mapList.Insert((int)DDKeys.D1, Keys.D1);
            mapList.Insert((int)DDKeys.D2, Keys.D2);
            mapList.Insert((int)DDKeys.D3, Keys.D3);
            mapList.Insert((int)DDKeys.D4, Keys.D4);
            mapList.Insert((int)DDKeys.D5, Keys.D5);
            mapList.Insert((int)DDKeys.D6, Keys.D6);
            mapList.Insert((int)DDKeys.D7, Keys.D7);
            mapList.Insert((int)DDKeys.D8, Keys.D8);
            mapList.Insert((int)DDKeys.D9, Keys.D9);
            mapList.Insert((int)DDKeys.D0, Keys.D0);
            mapList.Insert((int)DDKeys.Minus, Keys.OemMinus);
            mapList.Insert((int)DDKeys.Plus, Keys.Oemplus);
            mapList.Insert((int)DDKeys.Backslash, Keys.OemBackslash);
            mapList.Insert((int)DDKeys.BackSpace, Keys.Back);

            mapList.Insert((int)DDKeys.Tab, Keys.Tab);
            mapList.Insert((int)DDKeys.Q, Keys.Q);
            mapList.Insert((int)DDKeys.W, Keys.W);
            mapList.Insert((int)DDKeys.E, Keys.E);
            mapList.Insert((int)DDKeys.R, Keys.R);
            mapList.Insert((int)DDKeys.T, Keys.T);
            mapList.Insert((int)DDKeys.Y, Keys.Y);
            mapList.Insert((int)DDKeys.U, Keys.U);
            mapList.Insert((int)DDKeys.I, Keys.I);
            mapList.Insert((int)DDKeys.O, Keys.O);
            mapList.Insert((int)DDKeys.P, Keys.P);
            mapList.Insert((int)DDKeys.OpenBrackets, Keys.OemOpenBrackets);
            mapList.Insert((int)DDKeys.CloseBrackets, Keys.OemCloseBrackets);
            mapList.Insert((int)DDKeys.Enter, Keys.Enter);

            mapList.Insert((int)DDKeys.CapsLock, Keys.CapsLock);
            mapList.Insert((int)DDKeys.A, Keys.A);
            mapList.Insert((int)DDKeys.S, Keys.S);
            mapList.Insert((int)DDKeys.D, Keys.D);
            mapList.Insert((int)DDKeys.F, Keys.F);
            mapList.Insert((int)DDKeys.G, Keys.G);
            mapList.Insert((int)DDKeys.H, Keys.H);
            mapList.Insert((int)DDKeys.J, Keys.J);
            mapList.Insert((int)DDKeys.K, Keys.K);
            mapList.Insert((int)DDKeys.L, Keys.L);
            mapList.Insert((int)DDKeys.Semicolon, Keys.OemSemicolon);
            mapList.Insert((int)DDKeys.SingleQuote, Keys.OemQuotes);

            mapList.Insert((int)DDKeys.LShift, Keys.LShiftKey);
            mapList.Insert((int)DDKeys.Z, Keys.Z);
            mapList.Insert((int)DDKeys.X, Keys.X);
            mapList.Insert((int)DDKeys.C, Keys.C);
            mapList.Insert((int)DDKeys.V, Keys.V);
            mapList.Insert((int)DDKeys.B, Keys.B);
            mapList.Insert((int)DDKeys.N, Keys.N);
            mapList.Insert((int)DDKeys.M, Keys.M);
            mapList.Insert((int)DDKeys.Comma, Keys.Oemcomma);
            mapList.Insert((int)DDKeys.Period, Keys.OemPeriod);
            mapList.Insert((int)DDKeys.Question, Keys.OemQuestion);
            mapList.Insert((int)DDKeys.RShift, Keys.RShiftKey);

            mapList.Insert((int)DDKeys.LCtrl, Keys.LControlKey);
            mapList.Insert((int)DDKeys.LWin, Keys.LWin);
            mapList.Insert((int)DDKeys.LAlt, Keys.Alt);
            mapList.Insert((int)DDKeys.Space, Keys.Space);
            mapList.Insert((int)DDKeys.RWin, Keys.RWin);
            mapList.Insert((int)DDKeys.Apps, Keys.Apps);
            mapList.Insert((int)DDKeys.RCtrl, Keys.RControlKey);

            mapList.Insert((int)DDKeys.INS, Keys.Insert);
            mapList.Insert((int)DDKeys.HOME, Keys.Home);
            mapList.Insert((int)DDKeys.PageUp, Keys.PageUp);
            mapList.Insert((int)DDKeys.Delete, Keys.Delete);
            mapList.Insert((int)DDKeys.END, Keys.End);
            mapList.Insert((int)DDKeys.PageDown, Keys.PageDown);
            mapList.Insert((int)DDKeys.UP, Keys.Up);
            mapList.Insert((int)DDKeys.Left, Keys.Left);
            mapList.Insert((int)DDKeys.Down, Keys.Down);
            mapList.Insert((int)DDKeys.Right, Keys.Right);

            mapList.Insert((int)DDKeys.NumLock, Keys.NumLock);
            mapList.Insert((int)DDKeys.Divide, Keys.Divide);
            mapList.Insert((int)DDKeys.Multiply, Keys.Multiply);
            mapList.Insert((int)DDKeys.Subtract, Keys.Subtract);
            mapList.Insert((int)DDKeys.Add, Keys.Add);
            mapList.Insert((int)DDKeys.Enter2, Keys.Return);
            mapList.Insert((int)DDKeys.NumPad0, Keys.NumPad0);
            mapList.Insert((int)DDKeys.NumPad1, Keys.NumPad1);
            mapList.Insert((int)DDKeys.NumPad2, Keys.NumPad2);
            mapList.Insert((int)DDKeys.NumPad3, Keys.NumPad3);
            mapList.Insert((int)DDKeys.NumPad4, Keys.NumPad4);
            mapList.Insert((int)DDKeys.NumPad5, Keys.NumPad5);
            mapList.Insert((int)DDKeys.NumPad6, Keys.NumPad6);
            mapList.Insert((int)DDKeys.NumPad7, Keys.NumPad7);
            mapList.Insert((int)DDKeys.NumPad8, Keys.NumPad8);
            mapList.Insert((int)DDKeys.NumPad9, Keys.NumPad9);
        }

        public static Keys DDToCsharp(DDKeys ddCode)
        {
            return mapList[(int)ddCode];
        }
        public static DDKeys KeysToDD(Keys key)
        {
            var i = 0;
            foreach (var item in mapList)
            {
                if (item == key)
                {
                    return (DDKeys)i;
                }
                i++;
            }
            return DDKeys.None;
        }
        public static DDKeys ModifyKeyToDD(KeyModifiers modifyKey)
        {
            switch (modifyKey)
            {
                case KeyModifiers.Alt:
                    {
                        return DDKeys.LAlt;
                    }
                case KeyModifiers.Control:
                    {
                        return DDKeys.LCtrl;
                    }
                case KeyModifiers.Shift:
                    {
                        return DDKeys.LShift;
                    }
                case KeyModifiers.Windows:
                    {
                        return DDKeys.LWin;
                    }
                default:
                    {
                        return DDKeys.None;
                    }
            }
        }
    }
}
